<template>
  <div>
    <el-card class="box-1">
      <div slot="header"><span>企业展示</span></div>
      <el-form ref="form" :model="form" :rules="formRules" label-width="160px">
        <el-form-item label="企业名称：" prop="name">
          <el-input v-model="form['name']"/>
        </el-form-item>
        <el-form-item label="略缩图：">
          <div class="remark">*注:为保证显示效果,请上传一张750px*400px的图片</div>
          <ws-upload-single-img v-model="form['picture']" :path="form['picture']?ImageUrl+form['picture']:''"/>
        </el-form-item>
        <el-form-item label="企业简介：">
          <el-input v-model="form['introduce']" type="textarea" rows="3"/>
        </el-form-item>
        <el-form-item label="服务电话：">
          <el-input v-model="form['tel']"/>
        </el-form-item>
        <el-form-item label="联系地址：">
          <el-input v-model="form['address']" type="textarea" rows="3"/>
        </el-form-item>
        <div style="text-align:center">
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
          <el-button type="primary" @click="$router.back(-1)">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ImageUrl: '',
      // 新闻详情
      form: {},
      formRules: {
        name: [{ required: true, message: '请输入新闻标题', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getNews();
  },
  methods: {
    getNews() {
      const url = 'workorder/enterprise';
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.form = res.data;
      }).catch(e => {
        this.$message.error(e.msg)
      })
    },

    /**
     * 单提交方法,通过判断有没有form_id执行添加或编辑操作
     * formName: form表单中ref的值,即表单名
     */
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.form;
          const url = 'workorder/UpdateEnterprise';
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.$message.success(res.msg)
            this.$router.back(-1)
          }).catch((err) => {
            this.$message.error(err.msg)
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  max-width: 1000px;
  margin: 0 auto;
  > div span {
    font-size: 18px;
    font-weight: 600;
    padding: 0 10px;
  }
  .el-form {
    max-width: 900px;
    .title {
      font-weight: 600;
    }
    .remark {
      font-size: 13px;
      color: $textGray;
    }
    > div > .el-button {
      @extend .bg-green;
      width: 20%;
      min-width: 160px;
    }
  }
}
</style>
